#si pas le même redpertoire mettre -I a l'appelle de perl suivi du repertoire avec les packages
use strict;
use warnings;
use File::Basename;
use File::Spec;
package index_manager;


sub index_manager::creat_index {
	my $directoryName = $_[0];
	opendir(DIR, $directoryName) || die "Impossible d'ouvrir le dossier dans creat_index $directoryName\n";
	
	my @files = grep { /.txt$/ } readdir(DIR);
	closedir DIR;
	my ($mot, $cat, $lemme);
	my %index = ();
	my $filename;
	for my $file (@files) {
		open (TT, "\$TTDIR/tree-tagger-french $directoryName/$file 2>err.txt |") || die $!;
		$filename=$file;
		print "doing $filename\n";
		#retirer le .txt de la fin du fichier
		#$filename=basename($file,(".txt"));
		$filename=~s/[.]txt$//;
		my $pos=0;
		while (my $ligne = <TT>){
			if($ligne=~ /^(.*)\t(.*)\t(.*)$/){
				$mot=$1; $cat=$2; $lemme=$3;
	#print"$ligne";
				if(($cat !~ /^PUN/ )&&($cat ne "SENT")&&($cat ne "NUM")){
					if($lemme eq "<unknown>"){
						$lemme=$mot;
					}
					if($lemme eq "\@card\@"){
						$lemme=$mot;
					}
					$lemme=lc $lemme;
					my $ref_hDoc;
					if(exists($index{$lemme})){
						$ref_hDoc=$index{$lemme};
					}else{
						%{$ref_hDoc}=();
						$index{$lemme}=$ref_hDoc;
					}
					
					my $ref_hPos;
					if(exists($ref_hDoc->{$filename})){
						$ref_hPos=$ref_hDoc->{$filename};
					}else{
						@{$ref_hPos}=();
						$ref_hDoc->{$filename}=$ref_hPos;
					}
					
					push @{$ref_hPos} , $pos;
	#print"$lemme\t$filename\t$pos : @{$ref_hPos}\n";
	#foreach my $val (@{$ref_hPos}){
	#	print "$val\n";
	#}
				$pos++;
				}
			}
		}
		close TT;
	};
	return \%index;
}

sub index_manager::read_index {
	my $directoryName = $_[0];
	$directoryName=~s/.*[\/]([^\/]+)$/$1/;
	open (FIC_INDEX, "<./index_$directoryName.txt") || die "Impossible d'ouvrir le fichier dans read_index $directoryName\n";
	my %index = ();
	my $ref_hDoc;
	my $ref_hPos;
	while (my $ligne = <FIC_INDEX>){
		if($ligne=~ /^\#lemme\t(.*)$/){
			my %toto = ();
			$ref_hDoc=\%toto;
			$index{$1}=$ref_hDoc;
		}elsif($ligne=~ /^\#doc\t(.*)$/){
			my @toto = ();
			$ref_hPos=\@toto;
			$ref_hDoc->{$1}=$ref_hPos;
		}elsif($ligne=~ /^(.*)$/){
			push @{$ref_hPos} , $1;
		}
	}
	close FIC_INDEX;
	return \%index;
}

sub index_manager::write_index {
	my $directoryName = $_[0];
	$directoryName=~s/.*[\/]([^\/]+)$/$1/;
	my %index = %{$_[1]};
	open (OUT, ">./index_$directoryName.txt") || die $!;
	for my $klemme(sort { $a cmp $b } keys(%index)){
		print OUT "#lemme\t$klemme\n";
		my %hDoc = %{$index{$klemme}};
		for my $kdoc (keys(%hDoc)){
			print OUT "#doc\t$kdoc\n";
			my @hPos=@{$hDoc{$kdoc}};
			for my $val (@hPos){
				print OUT "$val\n";
			}
		}
	}
	close OUT;
}

sub index_manager::get_index {
	my $directoryName = $_[0];
	$directoryName=~s/.*[\/]([^\/]+)$/$1/;
	my %index;
	if (-e "./index_$directoryName.txt"){
		print"reading index\n";
		%index=%{&read_index($_[0])};
	}else{
		print"building index\n";
		%index=%{&creat_index($_[0])};
		print"writing index\n";
		&write_index($_[0],\%index);
	}
	return \%index;
}

1;
#&get_index($ARGV[0]);